#!/bin/bash

#assume internet (wifi driver 8188eu) available
#assume .netrc is set with google code for radiopi if want to commit/push
#assume we're now in ~/dev/

sudo apt-get update
sudo apt-get -y upgrade
sudo apt-get -y install g++-4.7 cmake git libgstreamer1.0-0 gstreamer1.0-tools libgstreamer-plugins-base1.0-0 gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-omx gstreamer1.0-alsa libupnp-dev hostapd udhcpd autoconf automake libtool libgstreamer1.0-dev libboost1.50-dev libglib2.0-0 libglib2.0-dev apache2 php5

#set gcc 4.7 as default
sudo ln -sf /usr/bin/gcc-4.7 /usr/bin/gcc
sudo ln -sf /usr/bin/g++-4.7 /usr/bin/g++


#remove dhcp server and hotspot from start at boot
sudo /etc/init.d/hostapd stop
sudo /etc/init.d/udhcpd stop
sudo update-rc.d hostapd remove
sudo update-rc.d udhcpd remove
#custom hostapd to work with rtl8188eu driver
wget http://andypi.co.uk/downloads/v1.1.tar.gz
tar -zxvf v1.1.tar.gz
cd RTL8188-hostapd-1.1/hostapd
sudo make
sudo make install
#not sure make install works
sudo cp hostapd /usr/local/bin/
#back to ~/dev/
cd ../..


#gmedia renderer
git clone https://github.com/hzeller/gmrender-resurrect.git
cd gmrender-resurrect
./autogen.sh
./configure
sudo make install
#sudo cp scripts/init.d/gmediarenderer /etc/init.d# no need using custom from radiopi git
#back to ~/dev/
cd ..
sudo rm -rf gmrender-resurrect

####clone and make radiopi
#mkdir dev
#cd dev
#git clone https://code.google.com/p/radiopi/
cd radiopi

############# replace config and startup scripts with pre-defined that work with radiopi
#config alsa to use DAC
sudo cp raspbian_config/asound.conf /etc/
#replace gmediarenderer shoudl use autoaudiosink rather than alsasink, initial volume is also raised
sudo cp raspbian_config/gmediarenderer /etc/init.d/
##set /etc/default/hostapd to point to config file /etc/hostapd/hostapd.conf
sudo cp raspbian_config/hostapd /etc/default/
#copy hostapd.conf et udhcpd.conf file, for hotspot wifi public open network
sudo cp raspbian_config/hostapd.conf /etc/hostapd/
#overwritte modules file to load i2s on boot
sudo cp raspbian_config/modules /etc/
#replace the file /etc/default/udhcpd and change the line: 
#comment DHCPD_ENABLED="no"
sudo cp raspbian_config/udhcpd /etc/default/
#copy udhcpd.conf for dhcp server
sudo cp raspbian_config/udhcpd.conf /etc/
#############

#make
mkdir build
cd build/
cmake ../
make
#start/Stop script
sudo cp ../raspbian_config/wifisoundsystem /etc/init.d/
sudo update-rc.d wifisoundsystem defaults

##sudo /etc/init.d/wifisoundsystem start
sudo shutdown -r now && exit


### remain more but not sure it's necessary : usb power limit

### NOT Tested
sudo apt-get install apache2 php5 libapache2-mod-php5
#set default virtualhost root directory
#(...)
#add ww-data user to pi group
sudo usermod -a -G pi www-data
#allow data file to be modified by group
chmod -R g+w data/
#add www-data to sudoers in /etc/sudoers :
#www-data ALL=(ALL) NOPASSWD: ALL



